package com.xhsj.user.sms.purchase.service;


import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.xhsj.user.sms.base.BaseService;
import com.xhsj.user.sms.purchase.entity.TbPurchaseSubtaskOtherFile;
import com.xhsj.user.sms.purchase.mapper.TbPurchaseSubtaskOtherFileMapper;
import com.xhsj.user.sms.purchase.mapper.TbPurchaseTaskFileMapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.io.Serializable;
import java.util.Collection;
import java.util.List;
import java.util.Map;
import java.util.Objects;

/**
 * <p>
 * 采购订单-采购订单其他文件 服务实现类
 * </p>
 *
 * @author suxiaolin
 * @since 2020-04-16
 */
@Slf4j
@Service
@Transactional(rollbackFor = Exception.class)
//@CacheConfig(cacheNames = "TbPurchaseOrderOtherFile")
public class TbPurchaseSubtaskOtherFileService extends BaseService<TbPurchaseSubtaskOtherFileMapper, TbPurchaseSubtaskOtherFile> {
    @Autowired
    private TbPurchaseSubtaskOtherFileMapper dao;

    @Autowired
    private TbPurchaseTaskFileMapper tbPurchaseTaskFileMapper;

    @Override
    //@CacheEvict(cacheNames="TbPurchaseOrderOtherFile", allEntries=true)
    public boolean save(TbPurchaseSubtaskOtherFile entity) {
        return super.save(entity);
    }

    @Override
    //@Cacheable(key = "#id",sync = true)
    public TbPurchaseSubtaskOtherFile getById(Serializable id) {
        return super.getById(id);
    }

    @Override
    //@CacheEvict(cacheNames="TbPurchaseOrderOtherFile", allEntries=true)
    public boolean removeById(Serializable id) {
        return super.removeById(id);
    }

    //@CacheEvict(cacheNames="SysDict", allEntries=true)
    @Override
    public boolean removeByIds(Collection<? extends Serializable> idList) {
        return super.removeByIds(idList);
    }

    //@Cacheable(key = "#data",sync = true)
    public IPage<TbPurchaseSubtaskOtherFile> selectPage(Map<String,Object> data) {
        int currentPage=1;
        if (!Objects.isNull(data.get("page"))) {
            currentPage=(Integer) data.get("page");
        }
        int pageSize=20;
        if (!Objects.isNull(data.get("size"))) {
            pageSize=(Integer) data.get("size");
        }

        TbPurchaseSubtaskOtherFile entity = JSON.parseObject(JSON.toJSONString(data), TbPurchaseSubtaskOtherFile.class);
        Page<TbPurchaseSubtaskOtherFile> page = new Page(currentPage,pageSize);
        QueryWrapper<TbPurchaseSubtaskOtherFile> wrapper = new QueryWrapper(entity);
        return super.selectPage(page, wrapper);
    }

    @Override
    //@Cacheable(value = "TbPurchaseOrderOtherFile",sync = true)
    public List<TbPurchaseSubtaskOtherFile> selectAll() {
        return super.selectAll();
    }

    //采购订单-采购其他相关文件
    public List<Map<String, Object>>  getPurchaseOrderOtherFileList(String dataId,List<String> typeList,List<String> nodeList) {
        return  dao.getPurchaseOrderOtherFileList(dataId, typeList, nodeList);
    }

    public List<Map<String,Object>> getInspectionApplicationFileList(String dataId, List<String> typeList, List<String> nodeList, String purchaseTaskId) {
        List<Map<String, Object>> purchaseOrderOtherFileList = dao.getPurchaseOrderOtherFileList(dataId, typeList, nodeList);
        QueryWrapper queryWrapper=new QueryWrapper<>();
        queryWrapper.select("file_name AS fileName,file_url AS fileUrl,type,remarks");
        queryWrapper.eq("purchase_task_id",purchaseTaskId);
        queryWrapper.eq("type",0);
        List list = tbPurchaseTaskFileMapper.selectMaps(queryWrapper);
        for (Map<String, Object> file:purchaseOrderOtherFileList
        ) {
            file.replace("type","1");
            list.add(file);
        }
        return  list;
    }
}
